#ifndef UDPCLIENT_H
#define UDPCLIENT_H

//#include <QtGui>
#include <QThread>
#include <QtNetwork/QUdpSocket>
#include <QHostAddress>
#include "stdafx.h"

//class MainWindow;
//QObject
class UdpClient : public QThread
{
    Q_OBJECT
public:
  //  UdpClient(MainWindow *parent);
    UdpClient();
    ~UdpClient();
    virtual void run();
    void stopRun();
    int mt;


public:
    QUdpSocket *udpSocket;
    quint16 port;
    double maxInChannel[6];

    double maxValueStd[6];

    typedef struct _ETC_FT3
    {
           char LNName;
           char DataSetName;
           unsigned short LDName;
           unsigned short EDXDL;
           unsigned short EDZXDL;
           unsigned short EDXDY;
           unsigned short EDYS;
           unsigned short statusWord1;
           unsigned short statusWord2;
           unsigned short RangeFlag;
           int freq;
    }ETC_FT3;
    ETC_FT3 etc_ft3;
    typedef struct _ETC_Duishi           //对时守时
    {
            char	   de_name[6];
            unsigned short    flag;   //标志位 0x2 表示对时数据 ;0x1 表示为守时数据
            double dTime;
    }ETC_Duishi;

    double m_BzArray[2560];
    double m_BcArray[2560];
    double m_tempBz[2560];
    double m_tempBc[2560];
    bool bcIsTest;
    bool bzOver;
    bool bcOver;
   // struct _ETC_S ETC_S;

 //   void setStop(bool Value){m_bStop = Value;}
    double getMaxPointValue(){return m_iShuZiLiangMax;}
    double getMaxPoint(){return m_dblMaxPoint;}
    double YouXiaoZhi(){return m_dblYouXiaoZhi;}
    double XiangWei(){return m_dblXW;}
    int getZhouQi(){return m_iZhouQi;}
    void ECED(float Value){m_fECED = Value;}
    float getECED(){return m_fECED;}
    bool getIsOK(){return m_isOK;}
    void setIsOK(bool value){m_isOK=value;}

    double getMaxPointValue_Bc(){return mxFt3Sample;}
    double getMaxPoint_Bc(){return m_dblMaxPoint_bc;}
    double YouXiaoZhi_Bc(){return m_dblYouXiaoZhi_bc;}
    double XiangWei_Bc(){return m_dblXW_bc;}
    int getZhouQi_bc(){return m_iZhouQi_bc;}
    void UdpSend(QString);
    void UdpSendByte(char *ch,int size);
    void UdpSendCmd(char *ch,int size);
    void setRead(bool value){m_bRead=value;}
    void setFuZhi(double value){m_dblFuZhi=value;}
    void setFuZhi_bc(double value){m_dblFuZhi_bc=value;}
    void setSuanFa(int value){iSuanFa=value;}
    double getPL(){return m_dblPL;}
    bool getSt(){return m_bSt;}
    void setSt(bool value){m_bSt=value;}
    int getYanshi(){return yanshi;}
    int getJgMin(){return jgMin;}
    int getJgMax(){return jgMax;}
    int getJgAvg(){return jgAvg;}
    void setCTPT(int value){m_iCTPT=value;}
    void setFT3lx(int value){m_iFT3lx=value;}
    int getFt3Xs();
    double getDs(){return m_dblDs;}    //取得对时误差
    double getSs(){return m_dblSs;}    //取得守时误差
    int getbwAll(){return bwAll;}
    int getbwLost(){return bwLost;}
    int getbwError(){return bwError;}
    int getLost(){return smpcnt;}
    int getBzFdbs(){return m_iBzFdbs;}        //放大倍数;
    int getBcFdbs(){return m_iBcFdbs;}        //放大倍数;

    void setFt3Channel(int index, int channel);

    double dblSampleMax_udp;
    int delay_specified;

    double dblSin[6];
    double dblCos[6];

    double dblYxzUdp[6];
    double dblXwUdp[6];

    double dblYxzUdp_ft3[6];
    double dblXwUdp_ft3[6];

    double sampleArray1[6][10*80];
    double sampleArray2[6][10*80];

    double (*sampleArray)[6][10*80];
    double (*showArray)[6][10*80];

    double sampleArray1_ft3[6][10*256];
    double sampleArray2_ft3[6][10*256];

    double (*sampleArray_ft3)[6][10*256];
    double (*showArray_ft3)[6][10*256];

    unsigned long int error1, error2,error3,error4;

    int channel_mapping[6];
    double maxFt3_inChannel[6];
    double mxFt3Sample;
    double dblSin_ft3[6];
    double dblCos_ft3[6];

    double ft3_factor_current;
    double ft3_factor_voltage;

    unsigned short delay_in_packet;

    int ft3SamplesPerCycle(){return m_dblMaxPoint_bc;}

    int setFt3Enable(bool enable){ isFt3 = enable;}

public slots:
    void dataReceived();
signals:
    void sigUdpDataIsOK();
    void sigUdpDataShow();
    void sigUdpMutIsReady();
    void ft3IsReady();

private:
    double rms_tmp;
    double freq_tmp;
    double phase_tmp;
    bool isFt3;
    int m_iBzFdbs;        //放大倍数;
    int m_iBcFdbs;        //放大倍数;
    int inum;
    int m_iCTPT;            //电流电压标志，0电流;1电压;
    int m_iFT3lx;           //FT3类型 0测量，1保护
    bool m_bRun;
    double m_dblSin;			// 实部
    double m_dblCos;			// 虚部
    int m_iTestedPoints;                // 标准 已获得测量点，用于标记绘图曲线的下标
    int m_iTestedPoints_bc;            // 被测 已获得测量点，用于标记绘图曲线的下标
    int m_iTestedZhouQi;                // 已获得周期数
    double iShuZiLiang;			// 数字量
    double m_dblYouXiaoZhi;		// 互感器基波有效值
    double m_dblXW;				// 互感器所示相位
    bool m_bSamplingPoints;		// 采点过程启动中
    double m_iShuZiLiangMax;			// 数字量最大值
    double m_iShuZiLiangMax_Temp;		// 数字量最大值_临时
    double m_dblMaxPoint;
    int m_iZhouQi;
    double m_dblYouXiaoZhi_bc;		// 互感器基波有效值
    double m_dblXW_bc;				// 互感器所示相位
    double m_iShuZiLiangMax_bc;			// 数字量最大值
    double m_iShuZiLiangMax_Temp_bc;		// 数字量最大值_临时
    double m_dblMaxPoint_bc;
    int m_iZhouQi_bc;
    double m_dblSin_bc;			// 实部
    double m_dblCos_bc;			// 虚部
    int m_iTestedZhouQi_bc;                // 已获得周期数
    double m_dblFuZhi;
    double m_dblFuZhi_bc;

    int yanshi;
    int jgMin,jgMax,jgAvg;
    int bwAll,bwLost,bwError;

    float m_fECED;			// 二次额定值
    bool m_isOK;
    bool m_bRead;
    double m_dblPL;         //频率
    bool m_bSt;           //状态标志，0,未进行任何操作，1,收到命令开始测试
    int smpcnt;

    double m_dblDs;         //对时
    double m_dblSs;         //守时

    void dft_14(double *samples,int M,int dft_N,int fs);

    void init();

    void ft3JianXi(unsigned short data[10],int freq);

    typedef struct _ETC_D
    {
            unsigned short       kind;           				/*数据类型  1表示标准通道，2表示被测通道，3表示ft3数据*/
            unsigned short	   shuru_xuhao;					/*例如标准通道选择1A为0 5A为1 大电压为2 模拟小电压为3*/
            unsigned short   sample_rate;      			/*通道采样率即采样频率*/
            double		xs;							/*模拟量进入后需要进行电路转换所进行的系数调整*/
            unsigned short        cycle;           			/*采样总周期数*/
            unsigned short        cycle_num;           		/*采样周期序数*/
            int                 data[256];				/*数据*/
            unsigned short       ft3_freq;        			/*ft3发送频率单位为MHz*/
            unsigned short       ft3_chnl;        			/*ft3通道*/
            unsigned short ft3_config[10];                      /*ft3参数*/
            unsigned short        ex[10];			/*备用*/
    }ETC_D;

    typedef struct _ETC_S
    {
            char	   de_name[6];
            unsigned short    flag;   //标志位 0x0 表示误差数据 ;0x1 表示为延时数据
            ETC_D	   ETC_DATA;
    }ETC_S;

    typedef struct _MUT_D
    {
            unsigned short      kind;           		/*数据类型  1表示标准通道，2表示被测通道，3表示ft3数据*/
            unsigned short      shuru_xuhao;			/*0 default*/
            unsigned short      sample_rate;      		/*通道采样率即采样频率*/
            double              xs;						/*transfer ratio of the hardware pre-AD*/
            unsigned short      cycle;           		/*采样总周期数*/
            unsigned short      cycle_num;           	/*采样周期序数*/
            int                 data[256];				/*数据*/
            unsigned short      ft3_freq;        		/*ft3发送频率单位为MHz*/
            unsigned short      ft3_chnl;        		/*ft3通道*/
            unsigned short      ft3_config[10];         /*ft3参数*/
            unsigned short      ex[10];                 /*备用*/
    }MUT_D;

    typedef struct _MUT_S
    {
        char            de_name[6];
        unsigned short  flag;               //标志位 0x0 表示误差数据 ;0x1 表示为延时数据
        MUT_D           MUT_Data;
    }MUT_S;

    typedef struct _ETC_Delay           //延时间隔时间
    {
            char	   de_name[6];
            unsigned short    flag;   //标志位 0x0 表示误差数据 ;0x1 表示为延时数据
            int delay;
            int jgMax;
            int jgMin;
            int bwAll;
            int bwLost;
            int bwError;
          //  short int step[1000];
    }ETC_Delay;

    typedef struct _packet_error           //延时间隔时间
    {
        char	   de_name[6];
        unsigned short    flag;   //标志位 0x0 表示误差数据 ;0x1 表示为延时数据
        long unsigned int	error_1;	// error less than 1us
        long unsigned int	error_2;	// error between 1~2 us
        long unsigned int	error_4;	// error between 2~3 us
        long unsigned int	error_6;	// error between 3~4 us
        long unsigned int	total;
        long unsigned int	lost;
        long unsigned int	disorder;
        long unsigned int   delay;
        long unsigned int   delay_defined;
    }packet_error;

  //  MainWindow *parent;
    int iSuanFa;
    int testFlag;
    bool m_bJQ;    
    double bzxs;
    double bcxs;
    void dataProcess(char*);
    void muDataProcess(char * ch);
    void delayProcess(char*);
    void dsProcess(char*);
    void ssProcess(char*);
    void packetAnalyzer(char*);
    void harmonic(double *s,int M,int len,int fs,int flag) ;  //s 所有的点 M：谐波次数（可以不用）len smrate*zhouqi fs smrate*50;flag =0标注，=1被测
    void harmonic(double *s,int len,int fs,int flag); //reload, to get freq of Ua only

    int ft3Points[6];
};

#endif
